package com.fmatos.amazingwallpapers.view.albums;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import org.joda.time.DateTime;

import android.util.Log;

import com.fmatos.amazingwallpapers.image.AsyncCloudManager;
import com.fmatos.amazingwallpapers.image.database.orm.Album;
import com.fmatos.amazingwallpapers.image.database.orm.Photo;
import com.fmatos.amazingwallpapers.image.database.orm.repository.AlbumRepo;
import com.fmatos.amazingwallpapers.image.database.orm.repository.PhotoRepo;
import com.j256.ormlite.dao.Dao;

/**
 * Responsible for bringing all data from DB and populating 
 * an {@link AlbumViewDto} object
 * @author fdematos
 *
 */
public class AlbumsViewSource {

	private static final String TAG = AlbumsViewSource.class.getSimpleName();

	private AsyncCloudManager asyncCloudManager;
	private AlbumRepo albumRepo;
	private PhotoRepo photoRepo;
//	private DatabaseHelper databaseHelper;

	
	
//	public AlbumsViewSource(AsyncCloudManager asyncCloudManager,DatabaseHelper databaseHelper) {
//		this.asyncCloudManager = asyncCloudManager;
//		this.databaseHelper = databaseHelper;
//	}
	
	
	@Inject
	public AlbumsViewSource(AsyncCloudManager asyncCloudManager, AlbumRepo albumRepo, PhotoRepo photoRepo) {
		super();
		this.asyncCloudManager = asyncCloudManager;
		this.albumRepo = albumRepo;
		this.photoRepo = photoRepo;
	}

	@Deprecated
	public void releaseResouces() {
	}

	
	public List<AlbumViewDto> getAlbumList() {
		
//		Dao<Album, String> albumDao = databaseHelper.getAlbumDao();
		List<Album> allAlbumsList = null;
		try {
//			allAlbumsList = albumDao.queryForAll();
			allAlbumsList = albumRepo.getAlbums();
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		
		LinkedList<AlbumViewDto> albumList = new LinkedList<AlbumsViewSource.AlbumViewDto>();

		if ( allAlbumsList == null ) {
			return null;
		}
		
//		Dao<Photo, String> photoDao = databaseHelper.getPhotoDao();
		
		Log.i(TAG,"Found " + allAlbumsList.size() + " albums");
		for ( Album album : allAlbumsList) {

			AlbumViewDto albumDto = new AlbumViewDto(album);
			albumList.add(albumDto);
			
			List<Photo> photos = photoRepo.getPhotosFromAlbum(album.getName());  //photoDao.queryForEq("albumName", album.getName());
			if( photos != null && photos.size() > 0) {
				Photo photo = photos.get(0);
				albumDto.thumbnailFile = photo.getFilePath();
				albumDto.thumbnailUrl = photo.getUrl();
				albumDto.photoCount = photos.size();
			}
				

			
		}

		return albumList;
	}
	
	private void fillDtoWithPhotoInfo(AlbumViewDto dto) {

//		RuntimeExceptionDao<AlbumRow, Integer> simpleDao = simpleHelper.getSimpleDataDao();
		// query for all of the data objects in the database
//		List<AlbumRow> list = simpleDao.queryForAll();
//		list = simpleDao.
		
//		for (AlbumRow simple : list) {
//			Log.d(TAG,"Simple dto "  + simple.toString());
//			dto.photoCount = simple.id;
//			dto.setUpdateTime(simple.date);
//		}
		
		
//		List<ImageRow> photoList = photosDataSource.getAllPhotosFromAlbum(dto.getName());
//		
//		if ( photoList != null ) {
//			if ( photoList.size() > 0 ) {
//				dto.thumbnailFile = photoList.get(0).getFile();
//				dto.thumbnailUrl = photoList.get(0).getUrl();
//			}
//			dto.photoCount = photoList.size();
//		}
//		
	}

	public  class AlbumViewDto {

		public String niceName;
		public String name;
		public String url;

		public boolean favorite;
		public DateTime updateTime;
		
		public String thumbnailFile;
		public String thumbnailUrl;
		
		public int photoCount;
		public boolean thumbnailIsDownloading; // tracks thubmnail download state

		
		public AlbumViewDto(Album album) {
			url = album.getUrl();
			niceName = album.getNiceName();
			name = album.getName();
			updateTime = new DateTime(album.getUpdateTime());
			
			favorite = album.isFavorite();

		}
		
	}

	public void loadThumbnailFromNetwork(AlbumViewDto albumDTO) {
		// TODO Auto-generated method stub
		
	}

	public void updateAlbumInfo(AlbumViewDto albumDto,Runnable runnableForUiThread) {

		Log.i(TAG," update album " + albumDto.name + "  from " + albumDto.url);
		
		asyncCloudManager.refreshAlbumInfo(albumDto.name,runnableForUiThread);
		
	}

	/**
	 * 
	 * @param albumDto
	 * @param isFavorite
	 * @return true if DB was updated appropriately
	 */
	public boolean setFavorite(AlbumViewDto albumDto,boolean isFavorite) {

		if ( albumDto.name == null) {
			return false;
		}
		
//		Dao<Album, String> albumDao = databaseHelper.getAlbumDao();
		
		try {
			Album album = albumRepo.getAlbum(albumDto.name); //albumDao.queryForId(albumDto.name);
			if ( album != null ) {
				album.setFavorite(isFavorite);
//				albumDao.update(album);
				albumRepo.updateAlbum(album);
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;
	}


}
